/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-24 00:34:58
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:03:48
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.ExaminationPaperDao;
import com.xtedu.examproject.entity.ExaminationPaper;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ExaminationPaperDaoImpl implements ExaminationPaperDao {
    @Override
    public int insert(ExaminationPaper examinationPaper) throws Exception {
        String sql = "insert into examination_papers (exam_id,question_id,create_date) values (?,?,?)";
        try (Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, examinationPaper.getExamId());
            ps.setInt(2, examinationPaper.getQuestionId());
            ps.setDate(3,examinationPaper.getCreateDate());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(ExaminationPaper examinationPaper) throws Exception {
        String sql = "update examination_papers set exam_id=? question_id=?,create_date=? where id=?";
        try (Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, examinationPaper.getExamId());
            ps.setInt(2, examinationPaper.getQuestionId());
            ps.setDate(3, examinationPaper.getCreateDate());
            ps.setInt(4, examinationPaper.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(ExaminationPaper examinationPaper) throws Exception {
        String sql = "delete from examination_papers where id=?";
        try (Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, examinationPaper.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public ExaminationPaper findById(int id) throws Exception {
        String sql = "select * from examination_papers where id=?";
        try (Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            ExaminationPaper examinationPaper = new ExaminationPaper();
            while (rs.next()) {
                examinationPaper.setId(rs.getInt("id"));
                examinationPaper.setExamId(rs.getInt("exam_id"));
                examinationPaper.setQuestionId(rs.getInt("question_id"));
                examinationPaper.setCreateDate(rs.getDate("create_date"));
            }
            return examinationPaper;
        }
    }

    @Override
    public List<ExaminationPaper> findAll() throws Exception {
        String sql = "select * from examination_papers";
        try (Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<ExaminationPaper> examinationPapers = new ArrayList<ExaminationPaper>();
            while (rs.next()) {
                ExaminationPaper examinationPaper = new ExaminationPaper();
                examinationPaper.setId(rs.getInt("id"));
                examinationPaper.setExamId(rs.getInt("exam_id"));
                examinationPaper.setQuestionId(rs.getInt("question_id"));
                examinationPaper.setCreateDate(rs.getDate("create_date"));
            }
            return examinationPapers;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }
}
